﻿using System;
using System.IO;

namespace Loader
{
    class Program
    {        
        private const string APPLICATION_EXECUTABLE = "LTemperaturos.exe";
        private const string APPLICATION_CONFIGURATION = "LTemperaturos.exe.config";
        private const string APPLICATION_NAME = "LTemperaturos";

        [LoaderOptimization(LoaderOptimization.MultiDomainHost)]
        [STAThread]
        static void Main(string[] args)
        {
            // Loader and LTemperaturos must reside in the same folder
#if DEBUG
            string statupPath = @"D:\Documents\Visual Studio 2008\Projects\LTemperaturos\LTemperaturos\bin\Debug";
#else
            string statupPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
#endif

            // this is where shadow copy will put all files
            string cachePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            // application config and executable absolute paths
            string configPath = Path.Combine(statupPath, APPLICATION_CONFIGURATION);
            string assemblyPath = Path.Combine(statupPath, APPLICATION_EXECUTABLE);

            // config shadow copy
            AppDomainSetup setup = new AppDomainSetup
                                   {
                                   	ApplicationName = APPLICATION_NAME,
                                   	ShadowCopyFiles = "true",
                                   	CachePath = cachePath,
                                   	ConfigurationFile = configPath
                                   };

        	// create application domain
            AppDomain domain = AppDomain.CreateDomain(APPLICATION_NAME, AppDomain.CurrentDomain.Evidence, setup);

            // start shadow copied application
            domain.ExecuteAssembly(assemblyPath);

            // after the application has finished, clean up
            AppDomain.Unload(domain);
            Directory.Delete(cachePath, true);
        }
    }
}
